package com.mall.shopping.services;

import com.mall.shopping.IRecommendService;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.RecommendResponse;
import com.mall.user.constants.SysRetCodeConstants;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Enzo Cotter on 2020/10/17.
 */
@Service
public class RecommendServiceImpl implements IRecommendService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    ItemMapper itemMapper;
    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        //推荐商品panelId为6
        //获取panel
        Panel panel = panelMapper.selectByPrimaryKey(6);
//        Example example = new Example(PanelContentItem.class);
//        example.createCriteria().andEqualTo("panelId",6);
//        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
//        //从数据库拿到的数据 和 返回数据bean不一致
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);
//        for (PanelContentItem panelContentItem : panelContentItems) {
//            //查 productName  salePrice  subTitle
//            Item item = itemMapper.selectByPrimaryKey(panelContentItem.getProductId());
//            panelContentItem.setProductName(item.getTitle());
//            panelContentItem.setSalePrice(item.getPrice());
//            panelContentItem.setSubTitle(item.getSellPoint());
//        }
        Set<Panel> panels = new HashSet<>();
        panel.setPanelContentItems(panelContentItems);
        panels.add(panel);
        recommendResponse.setPanelContentItemDtos(contentConverter.panels2Dto(panels));
        recommendResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        recommendResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return recommendResponse;

    }
}
